package com.lgmshare.k3.ui.base;

import android.view.View;
import android.widget.LinearLayout;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.lgmshare.component.widget.StatusLayout;
import com.lgmshare.component.widget.recyclerview.RecyclerViewAdapter;
import com.lgmshare.component.widget.recyclerview.XRecyclerView;
import com.lgmshare.k3.R;

import java.util.ArrayList;
import java.util.List;


/**
 * @author
 * @version V3.0.0
 * @Description: 简单列表activity
 * @date 2016/7/4 10:20
 */
public abstract class BaseListActivity<T> extends BaseActivity implements RecyclerViewAdapter.OnItemClickListener,
        RecyclerViewAdapter.OnItemChildClickListener {

    private static final int LOAD_SUCCESS = 0;
    private static final int LOAD_FAILURE = 1;

    protected LinearLayout mFixHeaderViewContainer;
    protected LinearLayout mFixFooterViewContainer;
    protected StatusLayout mStatusLayout;
    protected XRecyclerView mRecyclerView;
    protected RecyclerViewAdapter<T> mRecyclerAdapter;

    private int mPageIndex = 1;

    @Override
    protected int onBindLayoutResId() {
        return R.layout.activity_xrecyclerview;
    }

    @Override
    protected void initView() {
        //init header container
        mFixHeaderViewContainer = findViewById(R.id.layout_header_container);
        onInitFixHeaderView(mFixHeaderViewContainer);

        //init footer container
        mFixFooterViewContainer = findViewById(R.id.layout_footer_container);
        onInitFixFooterView(mFixFooterViewContainer);

        //init status layout and set view
        mStatusLayout = findViewById(R.id.status_layout);
        mStatusLayout.setLoadingMessage("");
        mStatusLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
        mStatusLayout.setErrorButtonClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onListReloadData();
            }
        });
        onInitStatusView(mStatusLayout);

        //init recyclerView
        mRecyclerView = findViewById(R.id.xrecycler_view);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity(), RecyclerView.VERTICAL, false));
        mRecyclerView.setPullRefreshEnable(true);
        mRecyclerView.setPullLoadEnable(false);
        mRecyclerView.setPullLoadAutoEnable(true);
        mRecyclerView.setXRecyclerViewListener(new XRecyclerView.XRecyclerViewListener() {
            @Override
            public void onRefresh() {
                pullRefresh();
            }

            @Override
            public void onLoadMore() {
                pullLoadMore();
            }
        });
        onInitXRecyclerView(mRecyclerView);

        mRecyclerAdapter = onListCreateAdapter();
        mRecyclerAdapter.setOnItemClickListener(this);
        mRecyclerAdapter.setOnItemChildClickListener(this);
        mRecyclerAdapter.registerAdapterDataObserver(new RecyclerView.AdapterDataObserver() {
            @Override
            public void onChanged() {
                super.onChanged();
                List<T> list = mRecyclerAdapter.getList();
                if (list == null || list.size() == 0) {
                    mStatusLayout.setEmptyMessage("没有数据");
                    mStatusLayout.showEmpty();
                }
            }
        });
        mRecyclerView.setAdapter(mRecyclerAdapter);
    }

    @Override
    protected void initLoad() {
        onListLoadData();
    }

    /**
     * 加载
     */
    protected void onListLoadData() {
        mPageIndex = 1;
        mStatusLayout.setLoadingMessage("正在加载数据...");
        mStatusLayout.showLoading();
        onListPullLoad(mPageIndex);
    }

    /**
     * 重新加载
     */
    protected void onListReloadData() {
        mStatusLayout.setLoadingMessage("正在加载数据...");
        mStatusLayout.showLoading();
        onListPullLoad(mPageIndex);
    }

    /**
     * pull网络请求成功处理
     *
     * @param list
     * @param totalSize
     */
    protected void onListPullLoadSuccess(List<T> list, int totalSize) {
        if (list == null) {
            list = new ArrayList<>();
        }
        if (mPageIndex == 1) {
            //刷新成功
            mRecyclerAdapter.setList(list);
        } else {
            //加载更多成功
            mRecyclerAdapter.addList(list);
        }

        List<T> dataList = mRecyclerAdapter.getList();
        //是否有下一页
        if (dataList == null && dataList.size() < totalSize) {
            mRecyclerView.setPullLoadEnable(true);
        } else {
            mRecyclerView.setPullLoadEnable(false);
        }

        //是否为空数据
        if (dataList == null || dataList.size() == 0) {
            mStatusLayout.showEmpty();
        } else {
            mStatusLayout.hide();
        }

        pullResultHandle(LOAD_SUCCESS);
    }

    /**
     * pull网络请求失败处理
     *
     * @param errorMsg
     */
    protected void onListPullLoadFailure(String errorMsg) {
        if (mPageIndex > 1) {
            mPageIndex--;
        }
        mStatusLayout.setErrorMessage(errorMsg);
        mStatusLayout.showError();
        pullResultHandle(LOAD_FAILURE);
    }

    /**
     * 执行下拉刷新操作
     */
    private void pullRefresh() {
        mPageIndex = 1;
        mRecyclerView.setPullLoadEnable(false);
        onListPullLoad(mPageIndex);
    }

    /**
     * 执行上拉加载更多操作
     */
    private void pullLoadMore() {
        mPageIndex++;
        onListPullLoad(mPageIndex);
    }

    private void pullResultHandle(int result) {
        mRecyclerView.onRefreshComplete();
        mRecyclerView.onLoadMoreComplete();
        switch (result) {
            case LOAD_FAILURE:
                break;
            case LOAD_SUCCESS:
                break;
            default:
                break;
        }
    }

    /**
     * add header
     *
     * @param headerContainerLayout
     */
    protected void onInitFixHeaderView(LinearLayout headerContainerLayout) {

    }

    /**
     * add footer
     *
     * @param footerContainerLayout
     */
    protected void onInitFixFooterView(LinearLayout footerContainerLayout) {

    }

    /**
     * set recyclerView
     *
     * @param recyclerView
     */
    protected void onInitXRecyclerView(XRecyclerView recyclerView) {

    }

    /**
     * set recyclerView
     *
     * @param statusLayout
     */
    protected void onInitStatusView(StatusLayout statusLayout) {

    }

    /**
     * adapter
     *
     * @return
     */
    protected abstract RecyclerViewAdapter onListCreateAdapter();

    /**
     * 执行刷新加载操作
     */
    protected abstract void onListPullLoad(int pageIndex);
}